/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Herramientas;

import java.util.LinkedList;
import modelos.Articulo;
import modelos.Autor;

/**
 *
 * @author JOSE
 */
public class Serializar {
    private String json = "";
    public static String serializaArticulos(LinkedList<Articulo> articulos){
        String json = "\"articulos\":[";
        for(int i = 0; i < articulos.size(); i++){
            Articulo articulo = articulos.get(i);

            if(i > 0){
                json += ",";
            }

            String jsonAutores = "[";

            for(int j = 0; j < articulo.getAutores().size(); j++){
                Autor autor = articulo.getAutores().get(j);
                if(j > 0){
                    jsonAutores += ",";
                }

                jsonAutores += "{"
                            +  "    \"idAutor\":" + "\"" + autor.getIdAutor() + "\""
                            +  "}";
            }

            jsonAutores += "]";

            json += "{" 
                            + "   \"idArticulo\": " + "\"" + articulo.getIdArticulo() + "\","
                            + "   \"fechaCreacion\": " + "\"" + articulo.getFechaCreacion() + "\","
                            + "   \"titulo\": " + "\"" + articulo.getTitulo() + "\","
                            + "   \"fechaCreacion\": " + "\"" + articulo.getFechaCreacion() + "\","
                            + "   \"resumen\": " 
                            + "                {"
                            + "                   \"idResumen\":" + "\"" + articulo.getResumen().getIdResumen() + "\","
                            + "                   \"contenidoResumen\":" + "\"" + articulo.getResumen().getContenidoResumen().replace("\r\n", " ").replace("\n", " ") + "\""
                            + "                },"
                            + "   \"autores\": " + jsonAutores
                            + "}";

        }
        json += "]";
        return json;
    }
}
